<?php

namespace app\manage\services;

use app\common\loader\ModelSystem;
use app\common\loader\Services;
use app\common\model\system\SystemVirtualPackageModel;
use think\db\exception\DbException;
use think\Exception;
use think\Model;

/**
 * 虚码套餐管理
 */
class VirtualPackageService extends BaseService
{

    /**
     * 虚码套餐列表
     * @param $option
     * @return void
     * @throws \think\Exception
     * @throws DbException
     */
    public function packageList($option): array
    {
        $page = ModelSystem::SystemVirtualPackage()->page($option);
        Services::Helper()->showListTitle($page['data'], [], ['status' => 'system.status']);
        return $page;
    }
    // 添加修改

    /**
     * 添加虚码套餐
     * @param array  $params
     * @param string $scene
     * @return mixed
     * @throws \think\Exception
     * @throws DbException
     */
    public function packageSave(array $params, string $scene)
    {
        $data = [
            'title' => $params['title'],
            'money' => $params['money'],
            'num' => $params['num'],
        ];
        $packageModel = ModelSystem::SystemVirtualPackage();
        if ($scene === 'add') {
            $data['create_at'] = date('Y-m-d H:i:s');
            $data['sort'] =  200;
            return $packageModel->insertGetId($data);
        }

        return $packageModel->update([['id', '=', $params['id']]], $data);
    }

    /**
     * 设置套餐状态
     * @param int $id
     * @param int $status
     * @return mixed
     * @throws DbException
     */
    public function editStatus(int $id, int $status)
    {
        return ModelSystem::SystemVirtualPackage()->update([['id', '=', $id]], ['status' => $status]);
    }

    /**
     * 套餐详情
     * @param $id
     * @return array|Model|null
     * @throws DbException
     */
    public function info($id)
    {
        $data = ModelSystem::SystemVirtualPackage()
            ->where('id','=', $id)
            ->find();
        if (empty($data)){
            throw  new Exception('信息未找到',API_ERROR);
        }
        return  $data;
    }
}
